<template>
	<view class="login-page" :style="my_style">
		<view class="bg-box">
			<!-- 表单 -->
			<view class="u-m-t-24 form">
				<input type="number" maxlength="11" v-model="mobile" class="u-m-b-20" placeholder="请输入手机号" />
				<view class="cell flex-between u-m-b-20">
					<input id="verify_code" type="number" maxlength="6" v-model="verify_code" placeholder="请输入短信验证码" />
					<text class="code-btn" @tap="showCode">{{ btnCodeText }}</text>
				</view>
				<input type="password" maxlength="20" v-model="password" class="u-m-b-20" placeholder="请设置登录密码" />
				<input type="password" maxlength="20" v-model="re_password" class="u-m-b-52" placeholder="请再次输入登录密码" />
				<view class="primary-btn flex-center u-m-b-12" @tap="$u.debounce(resetPassword, 2000, true)">确定</view>
			</view>
		</view>
		<!-- 图形验证码 -->
		<image-code :show.sync="show_img" :img_url="img_url" @change="imgCode" @confirm="confirmCode" />
	</view>
</template>
<script>
	import md5Libs from '@/utils/md5'
	import mixin from '@/mixin/captcha'
	import ImageCode from '@/components/common/ImageCode.vue'
	export default {
		mixins: [mixin],
		data() {
			return {
				active: 'login',
				is_agree: false,
				mobile: '', // 用户手机号
				verify_code: '', // 验证码
				password: '', // 密码
				re_password: '', // 确认密码
				show_img: false,
				img_url: '', // 图形验证码
			}
		},
		components: {
			ImageCode,
		},
		onLoad(options) {
			this.imgCode()
		},
		methods: {
			// 获取图形验证码
			showCode() {
				if (!this.mobile) {
					this.showMsg('请输入手机号码')
					return
				}
				if (!this.$u.test.mobile(this.mobile)) {
					this.showMsg('请输入正确的手机号码')
					return
				}
				this.show_img = true
			},
			// 确认输入图形验证码
			confirmCode(val) {
				console.log(val)
				this.show_img = false
				this.sendCaptcha(this.mobile, 'reset_pwd', val)
			},
			/**
			 * 用户-重置密码
			 */
			async resetPassword() {
				const {
					mobile,
					verify_code,
					password,
					re_password
				} = this
				if (!verify_code) {
					this.showMsg('请输入短信验证码')
					return
				}
				if (!this.$u.test.rangeLength(password, [6, 16])) {
					this.showMsg('请输入6-16位密码')
					return
				}
				if (!this.$u.test.rangeLength(re_password, [6, 16])) {
					this.showMsg('请输入6-16位密码')
					return
				}
				if (re_password !== password) {
					this.showMsg('两次输入密码不一致!')
					return
				}
				let params = {
					mobile,
					verify_code,
					password: md5Libs.md5(password),
					re_password: md5Libs.md5(re_password),
				}
				const res = await this.$api.common.user('resetPassword', params)
				if (res.code === 200) {
					this.successBack('重置密码成功')
				}
			},
		},
	}
</script>
<style lang="scss" scoped>
	.login-page {
		min-height: 100vh;
		padding: 100rpx 20rpx 66rpx;
		background: $uni-bg-color;

		.bg-box {
			width: 100%;
			height: 940rpx;
			padding: 28rpx 86rpx 40rpx;
			background-size: 100% 100%;
		}

		.tab-item {
			width: 50%;
			height: 100rpx;
			line-height: 100rpx;
			color: #666;
		}

		.form {
			input {
				height: 88rpx;
				border-bottom: 1px solid #ececec;
			}

			.cell {
				height: 88rpx;
				border-bottom: 1px solid #ececec;

				input {
					border-bottom: none;
				}

				.code-btn {
					padding: 6rpx 16rpx;
					font-size: 20rpx;
					line-height: 28rpx;
					color: #999;
					border: 1px solid #999;
					border-radius: 4rpx;
				}
			}
		}

		.theme-color {
			color: var(--color);
		}

		.primary-btn {
			width: 100%;
			height: 88rpx;
			font-size: 34rpx;
			color: #fff;
			background: var(--color);
			border-radius: 8rpx;
		}
	}
</style>
